package com.neusoft.service.impl;

import com.neusoft.mapper.CheckRequestMapper;
import com.neusoft.mapper.DisposalRequestMapper;
import com.neusoft.mapper.InspectionRequestMapper;
import com.neusoft.service.RegisterService;
import com.neusoft.entity.Register;
import com.neusoft.mapper.RegisterMapper;
import com.neusoft.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author akemihomurasama
 */
@Service
public class RegisterServiceImpl implements RegisterService {
    @Autowired
    private RegisterMapper registerMapper;
    @Autowired
    private CheckRequestMapper checkRequestMapper;
    @Autowired
    private InspectionRequestMapper inspectionRequestMapper;
    @Autowired
    private DisposalRequestMapper disposalRequestMapper;

    /**
     * 查询所有患者信息
     *
     * @return 患者信息列表
     */
    @Override
    public Result queryAllRegister() {
        return Result.ok(registerMapper.queryAllRegister());
    }

    /**
     * 添加患者信息
     *
     * @param register 患者信息
     */
    @Override
    public Result addPatient(Register register) {
        registerMapper.addPatient(register);
        return Result.ok();
    }

    /**
     * 根据医生查询患者
     *
     * @param employeeId 医生id
     * @return 患者列表
     */
    @Override
    public Result queryPatientByDoc(String employeeId) {
        return Result.ok(registerMapper.queryPatientByDoc(employeeId));
    }

    /**
     * 根据医生id分页查询患者信息
     *
     * @param employeeId  医生id
     * @param currentPage 当前页码
     * @param pageSize    每页数量
     * @return 患者列表
     */
    @Override
    public Result queryPatientByDocPage(String employeeId, int currentPage, int pageSize) {
        return Result.ok(registerMapper.queryPatientByDocPage(employeeId,
                (currentPage - 1) * pageSize,
                pageSize));
    }

    /**
     * 根据患者信息查询患者列表
     *
     * @param register 患者信息
     * @return 患者列表
     */
    @Override
    public Result queryPatientByCondition(Register register, int employeeId) {
        return Result.ok(registerMapper.queryPatientByCondition(register, employeeId));
    }

    /**
     * 根据患者病历号更改患者状态为2
     *
     * @param caseNumber 患者病历号
     */
    @Override
    public Result updatePatientState(String caseNumber) {
        registerMapper.updatePatientState(caseNumber);
        return Result.ok();
    }

    /**
     * 根据患者病历号查询患者信息
     *
     * @param caseNumber 患者病历号
     * @return 患者信息
     */
    @Override
    public Result queryPatientByCaseNum(String caseNumber) {
        return Result.ok(registerMapper.queryPatientByCaseNum(caseNumber));
    }

    /**
     * 根据患者看诊状态和医生id查询患者列表
     *
     * @param visitState 患者看诊状态
     * @param employeeId 医生id
     * @return 患者列表
     */
    @Override
    public Result queryRegisterByStateAndDoc(int visitState, int employeeId) {
        return Result.ok(registerMapper.queryRegisterByStateAndDoc(visitState, employeeId));
    }

    /**
     * 根据患者姓名或病历号查询患者信息
     *
     * @param register 患者相关信息
     * @return 患者信息
     */
    @Override
    public Result queryPatientByNameOrCaseNum(Register register) {
        Register register1 = registerMapper.queryPatientByNameOrCaseNum(register);
        if (register1 == null) {
            return Result.fail("查询的患者不存在");
        }
        return Result.ok(registerMapper.queryPatientByNameOrCaseNum(register));
    }

    /**
     * 根据患者id和医技项目id列表更新申请为已付款
     *
     * @param registerId 患者id
     * @param techIdList 医技项目id
     * @return 更新结果
     */
    @Override
    @Transactional
    public Result updateReqState(int registerId, List<Integer> techIdList) {
        disposalRequestMapper.updateReq(registerId, techIdList);
        checkRequestMapper.updateReq(registerId, techIdList);
        inspectionRequestMapper.updateReq(registerId, techIdList);
        return Result.ok();
    }

    /**
     * 根据接诊状态和id分页查询查询患者信息列表
     *
     * @param visitState     接诊状态
     * @param registerIdList 患者id列表
     * @param currentPage    当前页数
     * @param pageSize       每页数量
     * @return 患者信息列表
     */
    @Override
    public Result queryPatientByStateAndId(int visitState, List<Integer> registerIdList, int currentPage, int pageSize) {
        return Result.ok(registerMapper.queryPatientByStateAndId(visitState,
                registerIdList, (currentPage - 1) * pageSize, pageSize));
    }

    /**
     * 分页查询所有患者信息
     *
     * @param currentPage 当前页码
     * @param pageSize    每页数量
     * @return 患者列表
     */
    @Override
    public Result queryRegisterByPage(int currentPage, int pageSize) {
        return Result.ok(registerMapper.queryRegisterByPage((currentPage-1)*pageSize, pageSize));
    }

    /**
     * 查询患者总数
     *
     * @return 患者总数
     */
    @Override
    public Result queryPatientCount() {
        return Result.ok(registerMapper.queryPatientCount());
    }

    /**
     * 根据看诊状态分页查询患者信息
     *
     * @param state       看诊状态
     * @param currentPage 当前页码
     * @param pageSize    每页条数
     * @return 患者信息列表
     */
    @Override
    public Result queryPatientByStateAndPage(int state, int currentPage, int pageSize) {
        return Result.ok(registerMapper.queryPatientByStateAndPage(state, (currentPage-1)*pageSize, pageSize));
    }

    /**
     * 根据患者姓名、病历号查询已挂号的患者信息
     *
     * @param register 患者查询条件
     * @return 结果列表
     */
    @Override
    public Result queryPatientRefund(Register register) {
        register.setVisitState(1);
        return Result.ok(registerMapper.queryPatientByNameOrCaseNumAndState(register));
    }

    /**
     * 患者退号后修改患者看诊状态为已退号
     *
     * @param caseNumber 病历号
     * @return 修改结果
     */
    @Override
    public Result patientRefund(String caseNumber) {
        registerMapper.updateStateById(caseNumber, 4);
        return Result.ok();
    }

    /**
     * 根据条件查询患者数量
     *
     * @param register 查询条件
     * @return 患者数量
     */
    @Override
    public Result queryCountByCondition(Register register) {
        return Result.ok(registerMapper.queryCountByCondition(register));
    }

    /**
     * 患者完成看诊，设置患者状态为看诊结束
     *
     * @param caseNumber 患者病历id
     * @return 修改结果
     */
    @Override
    public Result patientAccomplish(String caseNumber) {
        registerMapper.updateStateById(caseNumber, 3);
        return Result.ok();
    }


}
